/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
 class Solution {
  public:
    bool hasCycle(ListNode *head) {
      auto dummy = new ListNode(0, head);
      auto p1 = dummy, p2 = dummy;
      do {
        if (p2->next == nullptr || p2->next->next == nullptr) {
          return false;
        }
        p1 = p1->next;
        p2 = p2->next->next;
      } while (p1 != p2);
      return true;
    }
  };